﻿using System.Net;
using Telex;

namespace Client1
{
    internal class Program
    {
        private static int index = 0;
        private static byte[] req = System.Text.Encoding.UTF8.GetBytes("请求=------哈哈哈环境科技");
        private static byte[] res = System.Text.Encoding.UTF8.GetBytes("响应=------一月又一月晕");

        static async Task Main(string[] args)
        {
            var cHost = new ClientBuilder()
                                        .ConfigureSecretkey("11")
                                        .AddSendProtocol(10, SendFunc)
                                        .AddCallProtocol(90, CallFunc)
                                        .ConfigureChannelActive(OnChannelActive)
                                        .ConfigureChannelInactive(OnChannelInactive)
                                        .Build();
            var remHost = await cHost.ConnectAsync(IPEndPoint.Parse("127.0.0.1:20001"), 10, new RpcHost("zz", "goutuizi"));


            bool isBool = true;
            var channel = cHost.GetChannel(remHost);

            while (true)
            {
                var str = Console.ReadLine();
                // Console.Clear();


                index++;
                // Console.WriteLine("第'" + index.ToString() + "'次");
                
                if (!string.IsNullOrEmpty(str))
                {
                    isBool = false;
                    break;
                }

                channel.Send(10, req);
                channel.Call(90, req, (response, opcode) =>
                {
                    Console.WriteLine($"响应--{index}>>data>>" + System.Text.Encoding.UTF8.GetString(response) + "    opcode>>" + opcode);
                });
            }
        }

        private static ValueTask OnChannelActive(RpcHost remotehost, ServiceKind kind)
        {
            Console.WriteLine("channel 连接::" + remotehost.ToString() + "        来源种类：" + kind.ToString());
            return ValueTask.CompletedTask;
        }

        private static ValueTask OnChannelInactive(RpcHost remotehost, ServiceKind kind)
        {
            Console.WriteLine("channel 断开::" + remotehost.ToString() + "        来源种类：" + kind.ToString());
            return ValueTask.CompletedTask;
        }

        private static void SendFunc(RpcHost host, uint protocolId, byte[] request)
        {
            throw new NotImplementedException();
        }

        private static void CallFunc(RpcHost host, uint protocolId, byte[] request, CallResponseDelegate func)
        {
            throw new NotImplementedException();
        }
    }
}
